package com.awakening.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Author:AwakeningCode
 * @Date: Created in 12:23 2018\1\23 0023
 */
@Service
public class UploadService {

    public String uploadImage(CommonsMultipartFile file,String uploadPath,String realUploadPath){
        InputStream is = null;
        OutputStream os = null;

        try {
            is = file.getInputStream();
            String des = realUploadPath + "/" + file.getOriginalFilename();
            os = new FileOutputStream(des);

            byte[] buffer = new byte[1024];
            int len = 0;
            //文件的字节上传，读取结束后，上传完毕
            while((len = is.read(buffer)) > 0){
                os.write(buffer);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (is != null){
                try {
                    is.close();
                }catch (Exception e2){
                    e2.printStackTrace();
                }
            }
            if (os != null){
                try {
                    os.close();
                }catch (Exception e3){
                    e3.printStackTrace();
                }
            }
        }

        return uploadPath + "/" +file.getOriginalFilename();
    }

}
